package com.youlu.campus.web.point;

import com.youlu.campus.entity.UserPO;
import com.youlu.campus.entity.VO.RankingListQueryVO;
import com.youlu.campus.service.ranking.UserActivityPointRankingProvinceService;
import com.youlu.campus.service.ranking.UserActivityPointRankingService;
import com.youlu.campus.service.visitor.VisitorUserService;
import com.youlu.campus.web.common.base.RequestContext;
import com.yuelin.infrastructure.quantum.common.QResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * 用户活动积分排行榜
 */
@RestController
public class UserActivityPointRankController {
    @Autowired
    private UserActivityPointRankingService userActivityPointRankingService;
    @Autowired
    private UserActivityPointRankingProvinceService pointRankingProvinceService;
    @Autowired
    private VisitorUserService visitorUserService;

    /**
     * 用户活动积分排行榜
     *
     * @param req
     * @return
     */
    @PostMapping("/mini-app/user-activity-point/rank-list")
    public QResult getPointDetail(@RequestBody RankingListQueryVO req) {
        req.setUserId(RequestContext.getUserId());
        req.setPlatform(RequestContext.getCurPlatform());
        req.setAppId(RequestContext.getAppId());
        return QResult.success(userActivityPointRankingService.getUserTaskRanking(req, RequestContext.getCurUser()));
    }

    /**
     * 用户活动积分排行榜
     *
     * @param req
     * @return
     */
    @PostMapping("/public/user-activity-point/rank-list")
    public QResult getPointDetailPublic(@RequestBody RankingListQueryVO req) {

        UserPO userPO = visitorUserService.findUser("default", "wechat-h5", "default");
        if (Objects.isNull(userPO)) {
            return QResult.fail("默认用户不存在");
        }
        req.setUserPO(userPO);
        req.setUserId(userPO.getId());
        req.setPlatform(RequestContext.getCurPlatform());
        req.setAppId(RequestContext.getAppId());
        return QResult.success(userActivityPointRankingService.getUserTaskRanking(req, RequestContext.getCurUser()));
    }

    /**
     * 用户活动积分省排行榜
     *
     * @param req
     * @return
     */
    @PostMapping("/mini-app/user-activity-point/province-rank-list")
    public QResult getProvinceRankList(@RequestBody RankingListQueryVO req) {
        req.setUserId(RequestContext.getUserId());
        req.setPlatform(RequestContext.getCurPlatform());
        req.setAppId(RequestContext.getAppId());
        String p = visitorUserService.getUserProvince(RequestContext.getCurUser(), req.getActivityId());
        req.setProvince(p);
        return QResult.success(pointRankingProvinceService.getUserTaskRanking(req, RequestContext.getCurUser()));
    }

    /**
     * 用户活动积分省排行榜
     *
     * @param req
     * @return
     */
    @PostMapping("/public/user-activity-point/province-rank-list")
    public QResult getProvinceRankListPublic(@RequestBody RankingListQueryVO req) {

        UserPO userPO = visitorUserService.findUser("default", "wechat-h5", "default");
        if (Objects.isNull(userPO)) {
            return QResult.fail("默认用户不存在");
        }
        req.setUserId(userPO.getId());
        req.setUserPO(userPO);
        req.setPlatform(RequestContext.getCurPlatform());
        req.setAppId(RequestContext.getAppId());
        String p = visitorUserService.getUserProvince(userPO, req.getActivityId());
        req.setProvince(p);
        return QResult.success(pointRankingProvinceService.getUserTaskRanking(req, RequestContext.getCurUser()));
    }
}
